/**
 * Created by Panda on 16/01/2017
 */
(function () {
    'use strict';

    var topBtns = [{
        key: "",
        icon: "checked",
        text: "审核通过",
        type:"reset"
    }, {
        key: "",
        icon: "cross",
        text: "审核不通过"
    }, {
        key: "",
        icon: "stop",
        text: "停用",
        type:"stop"
    }, {
        key: "",
        icon: "play",
        text: "启用",
        type:"reset"
    }, {
        key: "",
        icon: "et-return",
        text: "重置密码",
        type:"reset"
    }];

    angular
        .module('com.akxx.aqzyk')
        .controller('MemberController', MemberController);


    /** @ngInject */
    function MemberController($timeout, $state, SweetAlert,_Member) {
        var vm = this;
        vm.isReadonly = false;
        vm.newMember = false;
        vm.overlay = false;
        vm.checkItems = {};
        getMemberList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        function getMemberList(param) {
            _Member.MemberList.get(param).then(function (res) {
                vm.memberList = res.data;
            });
        };
        vm.topBtns = topBtns;

        //hover事件
        vm.memberItemShow = function(id){
            vm.isShow = id;
        }
        vm.memberItemHide = function(id){
            vm.isShow = "";
        }

        vm.toggleChecked = function (item) {
            if(item === "all"){
                var allCheck = vm.allChecked();
                angular.forEach(vm.memberList,function (item) {
                    item.check = !allCheck;
                });
            }else {
                item.check = !item.check;
            }
            vm.checkItems.push(item.check)
            console.log(item);
        };

        vm.allChecked = function () {
            var allCheck = vm.memberList && vm.memberList.length > 0;
            if(allCheck){
                for (var i in vm.memberList){
                    if(!vm.memberList[i].check){
                        allCheck = false;
                        break;
                    }
                }
            }
            if(allCheck){
                vm.checkItems = vm.memberList;
            }
            return allCheck;
        };
        //关闭弹窗
        vm.closeFrame = function(){
            vm.isReadonly = false;
            vm.newMember = false;
            vm.overlay = false;
        };

        //学生信息删除
        vm.memberItemDel = function(id){
            var param={id:id}
            _Member.DelMember.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功！");
                    getMemberList({async: true, pageSize: 0, pageIndex: 25, sortExpr: "name desc"});
                }else{
                }
            });
        }
        //查看信息
        vm.memberItemVis = function(id){
            var param={id:id}
            _Member.VisitMember.get(param).then(function (res) {
                if(res.success){
                    vm.memberForm = res.data;
                    vm.isReadonly = true;
                    vm.newMember = true;
                    vm.overlay = true;
                }
            });
        }
        //编辑
        vm.memberItemEdit = function(id){
            var param={id:id}
            _Member.VisitMember.get(param).then(function (res) {
                if(res.success){
                    vm.memberForm = res.data;
                    vm.newMember = true;
                    vm.overlay = true;
                }
            });
        }
        //保存
        vm.saveMember = function () {
            var param = vm.memberForm;
            param.id = id
            _Member.EditMember.get(param).then(function (res) {
                if (res.success) {
                    SweetAlert.swal("编辑成功！")
                    vm.closeFrame();
                }
            });
            getMemberList({async: true, pageSize: 0, pageIndex: 25, sortExpr: "name desc"});
        }

        // 头部按钮事件
        vm.topBtnEvent = function (id,type) {
            switch (type){
                case "reset":
                    vm.resetPwd(id);
                    break;
                case "start":
                    vm.startMem(id);
                    break;
                case "stop":
                    vm.stopMem(id);
                    break;
            }

        }
        // 重置密码
        vm.resetPwd = function (id) {
            var param=vm.checkItems;
            _Member.ResetMember.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("重置密码成功！")
                }
            });
        }
        // 启用
        vm.startMem = function (id) {
            var param=vm.checkItems;
            _Member.StartMember.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("启用成功！")
                }
            });
        }
        // 停用
        vm.stopMem = function (id) {
            var param=vm.checkItems;
            _Member.StopMember.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("停成功！")
                }
            });
        }
    }

})();

